/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.town.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.town.dto.PollTownDataAlterDto;
import com.hopes.bizz.poll.town.dto.TownHisDataDTO;
import com.hopes.bizz.poll.town.dto.TownHisTrendDTO;
import com.hopes.bizz.poll.town.entity.PollTownRtDataHisEntity;
import com.hopes.bizz.poll.town.service.PollTownRtDataHisService;
import com.hopes.bizz.poll.town.service.TownHisService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-09-26 18:57:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/townHis")
@Tag(description = "his", name = "${tableComment}管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollTownRtDataHisController {

    private final PollTownRtDataHisService pollTownRtDataHisService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param pollTownRtDataHis ${tableComment}
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('town_his_view')")
    public R getpollTownRtDataHisPage(Page page, PollTownRtDataHisEntity pollTownRtDataHis) {
        LambdaQueryWrapper<PollTownRtDataHisEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(pollTownRtDataHisService.page(page, wrapper));
    }


    /**
     * 通过id查询${tableComment}
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('town_his_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollTownRtDataHisService.getById(id));
    }

    /**
     * 新增${tableComment}
     *
     * @param pollTownRtDataHis ${tableComment}
     * @return R
     */
    @Operation(summary = "新增${tableComment}", description = "新增${tableComment}")
    @SysLog("新增${tableComment}")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('town_his_add')")
    public R save(@RequestBody PollTownRtDataHisEntity pollTownRtDataHis) {
        return R.ok(pollTownRtDataHisService.save(pollTownRtDataHis));
    }

    /**
     * 修改${tableComment}
     *
     * @param pollTownRtDataHis ${tableComment}
     * @return R
     */
    @Operation(summary = "修改${tableComment}", description = "修改${tableComment}")
    @SysLog("修改${tableComment}")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('town_his_edit')")
    public R updateById(@RequestBody PollTownRtDataHisEntity pollTownRtDataHis) {
        return R.ok(pollTownRtDataHisService.updateById(pollTownRtDataHis));
    }

    /**
     * 通过id删除${tableComment}
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除${tableComment}", description = "通过id删除${tableComment}")
    @SysLog("通过id删除${tableComment}")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('town_his_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollTownRtDataHisService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param pollTownRtDataHis 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('town_his_export')")
    public List<PollTownRtDataHisEntity> export(PollTownRtDataHisEntity pollTownRtDataHis) {
        return pollTownRtDataHisService.list(Wrappers.query(pollTownRtDataHis));
    }


    /**
     * 查询乡镇历史数据
     *
     * @param townHisDataDTO
     * @return R
     */
    @Operation(summary = "查询乡镇历史数据", description = "查询乡镇历史数据")
    @SysLog("查询乡镇历史数据")
    @GetMapping("queryTownHisData")
    @PreAuthorize("@pms.hasPermission('town_his_view','town_day_view','town_hour_view','town_min_view')")
    public R queryTownHisData(Page page, @Validated TownHisDataDTO townHisDataDTO) {
        return R.ok(SpringUtil.getBean(townHisDataDTO.getDataType(), TownHisService.class).queryTownHisData(page, townHisDataDTO));
    }

    /**
     * 实时数据趋势
     *
     * @param townHisTrendDTO
     * @return R
     */
    @Operation(summary = "实时数据趋势", description = "实时数据趋势")
    @SysLog("实时数据趋势")
    @GetMapping("trendAnalysis")
    @PreAuthorize("@pms.hasPermission('town_his_view','town_day_view','town_hour_view','town_min_view')")
    public R trendAnalysis(@Validated TownHisTrendDTO townHisTrendDTO) {
        return R.ok(SpringUtil.getBean(townHisTrendDTO.getTrendDataType(), TownHisService.class).trendAnalysis(townHisTrendDTO));
    }

    @Operation(summary = "修约历史数据", description = "修约历史数据")
    @SysLog("修约历史数据")
    @PutMapping("/alter")
    @PreAuthorize("@pms.hasPermission('poll_his_edit')")
    public R alter(@Validated @RequestBody PollTownDataAlterDto pollTownDataAlterDto) {
        SpringUtil.getBean(pollTownDataAlterDto.getDataType(), TownHisService.class).alter(pollTownDataAlterDto);
        return R.ok();
    }

}
